package com.house.framework.controller;

import com.house.exception.MyException;
import com.house.framework.dto.LoginDto;
import com.house.framework.service.ILoginService;
import com.house.framework.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName LoginController
 * @Version 1.0
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;
    /**
     *@Description 登录
     *@Param
     *@Return 
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = loginService.account(loginDto);
            return AjaxResult.me().setResultObj(map); // 把map返回给浏览器端
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常...");
        }
    }

    /**
     * wechat登录
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String, String> params) {
        try {
            return loginService.wechat(params);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常...");
        }
    }
    
    /**
     *@Description 绑定
     *@Param
     *@Return 
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String, String> param) {
        try {
            Map<String, Object> map = loginService.binder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常...");
        }
    }
}
